package cn.yunyichina.provider.pay.service.impl;

import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.pay.constant.ResponseEnum;
import cn.yunyichina.provider.pay.dao.BoneGuestDao;
import cn.yunyichina.provider.pay.model._do.BoneGuestDO;
import cn.yunyichina.provider.pay.model.vo.BoneGuestVO;
import cn.yunyichina.provider.pay.service.BoneGuestService;
import cn.yunyichina.utils.convert.JsonUtils;
import cn.yunyichina.utils.dozer.Dozer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 骨科大会客人签到
 * Created by Lullaby on 2017/6/23.
 */
@Service
public class BoneGuestServiceImpl implements BoneGuestService {

    private static final Logger logger = LoggerFactory.getLogger(BoneGuestServiceImpl.class);

    @Autowired
    private BoneGuestDao boneGuestDao;

    /**
     * 签到
     *
     * @param data
     * @return
     */
    public Response signBoneGuest(String data) {
        logger.info("BoneGuestServiceImpl.signBoneGuest.data -> {}", data);

        try {
            BoneGuestVO boneGuestVO = JsonUtils.parseObject(data, BoneGuestVO.class);
            if (StringUtils.isEmpty(boneGuestVO.getUsername()) ||
                    StringUtils.isEmpty(boneGuestVO.getPhone()) ||
                    StringUtils.isEmpty(boneGuestVO.getOrganization())) {
                return new Response().buildResultCodeAndMessage(
                        ResponseEnum.Common.INVALID.getCode(),
                        ResponseEnum.Common.INVALID.getMessage()
                );
            }

            Date sysdate = Calendar.getInstance().getTime();

            BoneGuestDO boneGuestDO = new BoneGuestDO();
            boneGuestDO.setUsername(boneGuestVO.getUsername());
            boneGuestDO.setPhone(boneGuestVO.getPhone());
            boneGuestDO.setOrganization(boneGuestVO.getOrganization());
            boneGuestDO.setPosition(boneGuestVO.getPosition());
            boneGuestDO.setSignTime(sysdate);
            boneGuestDO.setGmtCreate(sysdate);
            boneGuestDO.setGmtModified(sysdate);

            boneGuestDao.insert(boneGuestDO);
        } catch (Exception e) {
            logger.error("BoneGuestServiceImpl.signBoneGuest.Exception", e);
            return new Response().buildResultCodeAndMessage(
                    ResponseEnum.Common.FAILURE.getCode(),
                    ResponseEnum.Common.FAILURE.getMessage()
            );
        }

        return new Response();
    }

    /**
     * 查询
     *
     * @return
     */
    public Response listBoneGuest(String data) {
        logger.info("BoneGuestServiceImpl.listBoneGuest.data -> {}", data);

        List<BoneGuestVO> voList = new ArrayList<>();

        try {
            BoneGuestDO boneGuest = JsonUtils.parseObject(data, BoneGuestDO.class);
            List<BoneGuestDO> doList = boneGuestDao.select(boneGuest);

            for (BoneGuestDO boneGuestDO : doList) {
                BoneGuestVO boneGuestVO = Dozer.convert(boneGuestDO, BoneGuestVO.class);
                voList.add(boneGuestVO);
            }
        } catch (Exception e) {
            logger.error("BoneGuestServiceImpl.listBoneGuest.Exception", e);
            return new Response().buildResultCodeAndMessage(
                    ResponseEnum.Common.FAILURE.getCode(),
                    ResponseEnum.Common.FAILURE.getMessage()
            );
        }

        return new Response().buildResult(JsonUtils.toJsonString(voList));
    }

}
